/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#include "aboutdialog.h"

#include "IncFreeTrack.h"
#include "ftpushbutton.h"
#include "fttabwidget.h"
#include "ftshowtext.h"
#include "ftlabel.h"

AboutDialog::AboutDialog(QWidget *parent) :
    FreeTrackWidget(parent)
{
    // Create top area
    version = new FTLabel( QString("FreeTrack %1").arg( VERSION ) );
    version->setFont( QFont( "serif", 14, QFont::Black) );
    version->setAlignment( Qt::AlignCenter );

    author = new FTLabel( tr("Developed by Willy Gardiol") );
    author->setAlignment( Qt::AlignCenter );
    contact = new FTPushButton( QIcon( ":/icons/contact.png"), ICON_SIZE);
    contact->setBackground( QBrush( QColor(), Qt::NoBrush ) );
    connect( contact, SIGNAL(clicked()),
             this, SLOT(urlWillygardiol()));
    license = new FTLabel( tr("Distributed under GPLv3") );
    license->setAlignment( Qt::AlignCenter );

    close = new FTPushButton(tr("Back to FreeTrack"));
    connect( close, SIGNAL(clicked()),
              this, SIGNAL(accept()) );

    // Create the tabs widget...
    tabs = new FTTabWidget();

    // Create the "quickstart" tab
    // Tab quickstart
    tabQuickstart = new FTShowText(
 tr("<center|b>Welcome to FreeTrack!</center>"\
 "<justify>FreeTrack is an open-source (as in GPLv3) GPS tracking software for Symbian-base phones. "\
 "FreeTrack lets you record your movements and display them on a map. Also, FreeTrack lets you "\
 "load tracks in GPX format and show them on the map so that you can follow an already recorded track.</justify>"\
 "<justify>Actually, FreeTrack can do more than this. You can load your maps (in jpg or png formats), calibrate them "\
 "and display on the Tracking Table so that your tracks will be displayed on your maps. Also, you can upload your "\
 "tracks to a free online service called www.gobreadcrumbs.com with few easy clicks.</justify>"\
 "<justify>With FreeTrack you can do all this... and for free!</justify>"\
 "<center|b>Willy Gardiol</center>"\
 "<justify>The author of FreeTrack greatly appreciates any kind of support or donations if you like FreeTrack. Please "\
 "check on the 'support' tab on how to contribute to make this software even better.</justify>") );






    // tab "maps howto"
    tabMaps = new FTShowText( tr(
    "<center|b>How to use maps</center>"\
    "<justify>FreeTrack can load your raster maps, can help you calibrate them, and display them on "\
    "the Tracking Table. You can load either jpeg (jpg) files or png files. Note there is no "\
    "limit in image sizes.</justify>"\
    "<center|b>Map Calibration</center>"\
    "<justify>FreeTrack is able to import calibration files from Ozi Explorer and the CompeGPS (and TwoNav) "\
    "software products. First, load your map on the layers page then use the 'load calibration' icons "\
    "to locate and import the MAP (OziExplorer) or the IMP (CompeGPS) calibration files. If you prefer, "\
    "you can rename the calibration file to match the map filename, place both files in the same folder "\
    "and FreeTrack will automatically import the calibration for you.</justify>"\
    "<justify>If you do not have any calibration files, you can still calibrate your map within FreeTrack. "\
    "First you need to load the map, then from the edit button, you can access the calibration tab where "\
    "you will see the entire map. Now, place three calibration points on the map using the buttons and "\
    "panning/zooming the map around, then open the 'points' tab and enter the lat/lon (WGS84, decimal degrees) "\
    "cohordinates for the three points.</justify>"\
    "<justify>Once the map is calibrated, you will see it on the Tracking Table.</justify>"\
    "<center|b>Troubleshooting</center>"\
    "<justify>If your map does not show up but all you can see are red crosses, you need to clear and regenerate "\
    "the map cache. You can do this from the map edit dialog box, accessible from the layers page.</justify>"\
    "<center|b>Implementation Details</center>"\
    "<justify>FreeTrack will 'split' your map in smaller images, for faster loading and store those files "\
    "into the 'map_cache' folder under the FreeTrack folder. You can regenerate or delete the cache by "\
    "clicking on the buttons in the map edit dialog.</justify>") );

    // tab "tracks howto"
    tabTracks = new FTShowText( tr(
    "<center|b>How to use Tracks</center>"\
    "<justify>FreeTrack can record Live Tracks and load tracks in GPX format.</justify>"\
    "<center|b>Live Tracks</center>"\
    "<justify>A track which is being recorded is called a Live track. The Live Track can be autosaved by "\
    "activating the feature from the configuration page, in this case you only have to specify a filename "\
    "for the track and then FreeTrack will automatically save it for you, so you will never lose it even if "\
    "the battery dies!</justify>"\
    "<justify>You can also load a previously saved track as the current Live Track, allowing you to save and "\
    "resume a Live Track, in case you need to switch the phone off, or continue an old track you left off</justify>"\
    "<center|b>Loading a track</center>"\
    "<justify>FreeTrack can import any track in GPX format. Just switch to the layers page and tap the load track "\
    "button. The track will be displayed on the tracking table so you can see it toghether with your maps and "\
    "Live track.</justify>"\
    "<justify>To make it easy to see the tracks and identify them when you have loaded many, "\
    "you can configure both color and thickness for each track. Do do this, switch to the layers page "\
    "and tap on the track edit button, from here you can customize your track. Track editing is NOT supported by "\
    "FreeTrack at the moment.</justify>"\
    "<justify>The color and thickness information is stored in an external track configuration file, "\
    "so your GPX file will not be modified by FreeTrack.</justify>") );

    // Create about tab
    tabAbout = new QWidget();
    aboutText = new FTShowText( tr( "<center|i>To Biancamaria. Time is gone and I wish we had more.</center>" \
                                    "<center|b>Please support FreeTrack!</center>" \
                                    "<center>Code and project by Willy Gardiol</center>"\
                                    "<center>Icons by Simograndi</center>"\
                                    "<center|b>Localizations:</center>" \
                                    "<center>Italian: by Willy Gardiol</center>"\
                                    "<center>German: by Alex Schmitz</center>"\
                                    "<center>French: by Roberto Trivero</center>"\
                                    "<center>Arabic: by Roberto Trivero</center>"\
                                    "<center>Chinese: by Tao Huang</center>"\
                                    "<center>Dutch: by Erwin Homan</center>"\
                                    "<center>Spanish: by Miguel Bermejo</center>"\
                                    "<center>Polish: by Michal Komala</center>"\
                                    "<justify|i>Many thanks Nokialino for supporting Freetrack!</justify>" ) );

    aboutQt = new FTPushButton( QIcon( ":/icons/qt-logo.png"), ICON_SIZE);
    connect( aboutQt, SIGNAL(clicked()), this, SLOT(infoqt()));
    simograndi = new FTPushButton(QIcon( ":/icons/simograndi.png"), ICON_SIZE);
    connect( simograndi, SIGNAL(clicked()), this, SLOT(urlSimograndi()));
    openNokialino = new FTPushButton( QIcon( ":/icons/nokialino.png"), ICON_SIZE );
    connect( openNokialino, SIGNAL(clicked()), this, SLOT(urlNokialino()) );

    // Create supprot tab
    tabSupport = new QWidget();
    supportText = new FTShowText( tr(
    "<center>To get support, go to FreeTrack homepage:</center>"\
    "<center|i>http://code.google.com/p/qtfreetrack</center>"\
    "<center>Or write to:</center>"\
    "<center|i>willy@gardiol.org</center>"\
    "<center|i>This is Free Software (GPLv3), please contribute!</center>"\
    "<justify>Code, graphics, testing... even money, if you really like FreeTrack, is appreciated and will "\
    "help support further FreeTrack development.</justify>"\
    "<center|b>For donations, you can use my PayPal account which is: willy@gardiol.org.</center>") );

    home = new FTPushButton( QIcon( ":/icons/freetrack_48.png"), ICON_SIZE );
    connect( home, SIGNAL(clicked()),
             this, SLOT(urlHomepage()));
    support = new FTPushButton( QIcon( ":/icons/bugs.png"), ICON_SIZE);
    connect( support, SIGNAL(clicked()),
             this, SLOT(urlIssues()));
    paypal = new FTPushButton( QIcon( ":/icons/paypal.png"), ICON_SIZE );
    connect( paypal, SIGNAL(clicked()),
             this, SLOT(urlPaypal()));

    // Add tabs
    tabs->addTab( tabQuickstart, tr("Quickstart..."), QIcon(":/icons/quickstart.png"), QSize(150,50));
    tabs->addTab( tabMaps, tr("Maps how to...") /*, QIcon(":/icons/help-maps.png"), QSize(150,50)*/);
    tabs->addTab( tabTracks, tr("Tracks how to...") /*, QIcon(":/icons/help-tracks.png"), QSize(150,50)*/);
    tabs->addTab( tabAbout, tr("About FreeTrack!"), QIcon(":/icons/about-freetrack.png"), QSize(150,50) );
    tabs->addTab( tabSupport, tr("Get Support!"), QIcon(":/icons/support.png"), QSize(150,50));

    tabs->addExtra( close );
}

AboutDialog::~AboutDialog()
{
}

void AboutDialog::infoqt()
{
    QMessageBox::aboutQt(this);
}

void AboutDialog::openUrl(QString url)
{
    QDesktopServices::openUrl(QUrl(url));
}

void AboutDialog::urlNokialino()
{
    openUrl( "http://www.nokialino.it");
}

void AboutDialog::urlSimograndi()
{
    openUrl( "http://simograndi.wordpress.com" );
}

void AboutDialog::urlHomepage()
{
    openUrl( "http://code.google.com/p/qtfreetrack/");
}

void AboutDialog::urlIssues()
{
    openUrl( "http://code.google.com/p/qtfreetrack/issues/");
}

void AboutDialog::urlWillygardiol()
{
    QMessageBox::information(this, tr("Contact Willy Gardiol"),
                                   tr("To contact Willy Gardiol, please send an email to: <willy@gardiol.org>!"));
}

void AboutDialog::urlPaypal()
{
    openUrl( "http://www.paypal.com");
}

QLayout* AboutDialog::createHorizontal()
{
    QVBoxLayout* grid = new QVBoxLayout();
    grid->addWidget( version );
    QHBoxLayout* authorgrid = new QHBoxLayout();
    authorgrid->addWidget( author );
    authorgrid->addWidget( contact );
    authorgrid->addWidget( license );
    grid->addLayout( authorgrid );
    grid->addWidget( tabs );

    // Build about tab
    QHBoxLayout* aboutGrid = new QHBoxLayout();
    QVBoxLayout* aboutButtonsGrid = new QVBoxLayout();
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( aboutQt );
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( simograndi );
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( openNokialino);
    aboutButtonsGrid->addStretch();
    aboutGrid->addLayout( aboutButtonsGrid );
    aboutGrid->addWidget( aboutText );
    if ( tabAbout->layout() )
        delete tabAbout->layout();
    tabAbout->setLayout( aboutGrid );

    // Build support tab
    QHBoxLayout* supportGrid = new QHBoxLayout();
    QVBoxLayout* supportButtonsGrid = new QVBoxLayout();
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( home );
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( support );
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( paypal);
    supportButtonsGrid->addStretch();
    supportGrid->addLayout( supportButtonsGrid );
    supportGrid->addWidget( supportText );
    if ( tabSupport->layout() )
        delete tabSupport->layout();
    tabSupport->setLayout( supportGrid );

//    grid->addWidget( close );
    return grid;
}

QLayout* AboutDialog::createVertical()
{
    QVBoxLayout* grid = new QVBoxLayout();
    grid->addWidget( version );
    QHBoxLayout* authorgrid = new QHBoxLayout();
    authorgrid->addWidget( author );
    authorgrid->addWidget( contact );
    grid->addLayout( authorgrid );
    grid->addWidget( license );
    grid->addWidget( tabs );

    // Build about tab
    QVBoxLayout* aboutGrid = new QVBoxLayout();
    QHBoxLayout* aboutButtonsGrid = new QHBoxLayout();
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( aboutQt );
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( simograndi );
    aboutButtonsGrid->addStretch();
    aboutButtonsGrid->addWidget( openNokialino);
    aboutButtonsGrid->addStretch();
    aboutGrid->addLayout( aboutButtonsGrid );
    aboutGrid->addWidget( aboutText );
    if ( tabAbout->layout() )
        delete tabAbout->layout();
    tabAbout->setLayout( aboutGrid );

    // Build support tab
    QVBoxLayout* supportGrid = new QVBoxLayout();
    QHBoxLayout* supportButtonsGrid = new QHBoxLayout();
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( home );
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( support );
    supportButtonsGrid->addStretch();
    supportButtonsGrid->addWidget( paypal);
    supportButtonsGrid->addStretch();
    supportGrid->addLayout( supportButtonsGrid );
    supportGrid->addWidget( supportText );
    if ( tabSupport->layout() )
        delete tabSupport->layout();
    tabSupport->setLayout( supportGrid );

    return grid;
}
